package com.oracle.application.controller;


import com.oracle.application.entity.vo.OrderConfirmVo;
import com.oracle.application.entity.vo.OrderSubmitVo;
import com.oracle.application.entity.vo.OrdersVo;
import com.oracle.application.entity.vo.R;
import com.oracle.application.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 莉丝妲黛
 * @since 2021-06-24
 */
@Slf4j
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService iOrdersService;


    /**
     * 订单确认的订单信息
     *
     * @return
     */
    @GetMapping("/confirm")
    public R<OrderConfirmVo> confirm() {
        OrderConfirmVo orderConfirm = iOrdersService.createOrderConfirm();
        return new R<>(200, "success", orderConfirm);
    }

    /**
     * 创建订单信息
     *
     * @param orderSubmitVo 订单详系内容
     * @return
     * @RequestBody 接收数据为json
     */
    @PostMapping("/submit")
    public R submit(@RequestBody OrderSubmitVo orderSubmitVo)throws  Exception {
        log.info("订单提交后的详细内容:参数{}", orderSubmitVo);
        Map<String, Object> map = this.iOrdersService.submitOrder(orderSubmitVo);
        final int i = 500;
        if (map.get("code") != null && map.get("code").equals(i)) {
            return new R(201, "请不要重复下单", null);
        }
        OrdersVo ordersVo = (OrdersVo) map.get("date");
        return new R(200, "success", ordersVo);
    }
}
